import java.net.*; 
import java.io.*;

public class Listener implements Runnable {

	private Sender sender;
	private int port;
	
	public Listener(int port, Sender sender) {
		this.sender = sender;
		this.port = port;
	}

	public void run() {
		try {
			startListener(port);
		} catch (IOException e) {
			System.out.println("couldn't start listening on port " + port + ": " + e);
		}
		System.out.println("listener gestartet");
	}
	
	private void startListener(int port) throws IOException {
		ServerSocket anschluss = new ServerSocket(port);
		while (true) {
			Socket client = anschluss.accept();
			sender.setRecipient( client.getInetAddress().toString().substring(1) );
			BufferedReader Eingabe = new BufferedReader( new InputStreamReader(client.getInputStream()) );
			System.out.println("empfangen: " + Eingabe.readLine());
		}
	}

}